package test.util;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.util.Map;

/**
 * @author zhenjie.Ji
 * @description 类描述
 * @date 2024/12/17 10:23
 */
public class HttpClient {

    /**
     * get 请求封装
     *
     * @param url url
     * @return 返回的请求数据
     */
    public static String get(String url) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpUriRequestBase request = new HttpGet(url);
        CloseableHttpResponse response = client.execute(request);
        int code = response.getCode();
        HttpEntity entity = response.getEntity();
        String s = EntityUtils.toString(entity);
        return s;
    }

    /**
     * post 请求封装
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param body    请求体
     * @return 响应数据
     */
    public static String post(String url, Map<String, String> headers, String body) {
        String result = "";
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpUriRequestBase request = new HttpPost(url);
            if (!(headers.isEmpty())) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    request.setHeader(entry.getKey(), entry.getValue());
                }
            }
            request.setEntity(new StringEntity(body));
            CloseableHttpResponse response = client.execute(request);
            int code = response.getCode();
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * post 请求封装
     *
     * @param url  请求地址
     * @param body 请求体
     * @return 响应数据
     */
    public static String post(String url, String body) {
        String result = "";
        try {
            System.out.println("请求发出：" + body);
            CloseableHttpClient client = HttpClients.createDefault();
            HttpUriRequestBase request = new HttpPost(url);
            // 设置 Content-Type
            request.setHeader("Content-Type", "application/json");
            request.setEntity(new StringEntity(body));
            CloseableHttpResponse response = client.execute(request);
            int code = response.getCode();
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

//
//    public static void main(String[] args) throws Exception {
//        String s = get("https://idmapi-test.cavanauto.com/oauth2/client_token?grant_type=client_credentials&client_id=zYIkRbXmak&client_secret=89bX2KK3kw");
//        System.out.println(s);
//    }
}
